/**
 * data: 2023-12-20 16:46:58
 *
 */
require(["cat", "vue", "common", "api", "dialog"], function (
  cat,
  Vue,
  app,
  api,
  dialog
) {
  let that,
    callback =
      app.getValue("callback") || app.getValue("callback", "session") || ""; //回去标识  不一定是从那来回哪去;
  const HEIGTH =
    window.innerHeight > 0
      ? window.innerHeight
      : document.documentElement.clientHeight;

  app.storeValue("callback", callback, "session"); //去授权前将回去标识存在缓存

  // 限制 无参数进入
  if (!callback) {
    dialog.tipDialog("参数错误,请重试");
    return;
  }
  // var urls = app.getValue('callBackUrl')||'';
  // app.storeValue('url', urls, 'session')
  let speed = 60;
  let fromStr = "weizan";

  let token = app.getValue("token", "local");

  let wxcode = app.getValue("wxcode", "session") || "20240111";

  //判断微信打开
  cat.isWeChat(() => {
    var vm = new Vue({
      el: "#app",
      data: {
        screenH: HEIGTH,
        phone: "",
        code: "",
        wxcode: wxcode,
        speed: speed,
        utime: speed,
        isSelect: false, // 是否已经选择
        isNot: false, // 是否授权过
      },
      created() {
        that = this;
      },
      mounted() {
        // cat.wxWarranty();
        if (!token) {
          this.isNot = true;
        } else {
          // 有token
          this.checkIsBind();
        }
      },
      methods: {
        /**
         * 检查是否已经有 token 如果有请求后返回.
         */
        checkIsBind() {
          api.checkPhoneLogin(token, fromStr).then((res) => {
            if (res.code == 200) {
              // 判定
              app.storeValue("token", res.data.token, "local");

              // 返回
              window.location.href =
                cat.checkBackUrl(callback) + res.data.resultStr;
            } else if (res.code == 801) {
              vm.isNot = true;
              app.deleteValue("token");
            } else {
              res.msg && dialog.tipDialog(res.msg);
            }
          });
        },
        /**
         * 获取验证码倒计时
         */
        getCodeCountdown() {
          // let utime = 60;
          const timer = setInterval(() => {
            if (that.utime <= 0) {
              clearInterval(timer);
              that.utime = speed;
            } else {
              that.utime--;
            }
            // that.utime = utime;
          }, 1000);
          // 请求 后台.

          console.log("倒计时开始 ,并请求后台");
        },
        linkTo(str) {
          app.linkTo("agreement");
        },
        /**
         *  发送短信
         */
        sendMessage() {
          api.sendMessage(that.phone, that.wxcode).then((res) => {
            console.log(res);
            if (res.code == 200) {
              dialog.tipDialog("发送成功,请注意查收");
            } else {
              res.msg && dialog.tipDialog(res.msg);
            }
          });
        },
        /**
         * 获取验证码
         */
        clickGetCode() {
          let that = this;
          let phone = that.phone;

          if (phone == "") {
            dialog.tipDialog("请输入手机号");
            return;
          }
          if (!cat.validatePhoneNumber(phone)) {
            dialog.tipDialog("请输入正确的手机号");
            return;
          }
          if (that.utime < speed && that.utime > 0) {
            dialog.tipDialog("请稍等");
            return;
          }

          that.utime--;
          that.getCodeCountdown();
          that.sendMessage();
        },
        /**
         * 是否勾选
         */
        clickSelect() {
          that.isSelect = !that.isSelect;
        },
        /**
         * 验证输入的数据
         */
        valInputData() {
          if (that.phone == "") {
            dialog.tipDialog("请输入手机号");
            return false;
          }
          if (!cat.validatePhoneNumber(that.phone)) {
            dialog.tipDialog("请输入正确的手机号");
            return false;
          }
          if (that.code == "") {
            dialog.tipDialog("请输入验证码");
            return false;
          }
          if (that.isSelect == false) {
            dialog.tipDialog("请阅读并勾选用户协议");
            return false;
          }
          return true;
        },

        /**
         * 提交
         */
        submitBtn() {
          // cat.wxWarranty();
          if (this.valInputData()) {
            api.phoneLogin(that.phone, that.code, fromStr).then((res) => {
              if (res.code == 200) {
                // 判定
                app.storeValue("token", res.data.token, "local");

                // 返回
                window.location.href =
                  cat.checkBackUrl(callback) + res.data.resultStr;
              } else {
                res.msg && dialog.tipDialog(res.msg);
              }
              // console.log(res);
            });

            // dialog.tipDialog("提交数据");
          }
        },
      },
    });
  });
});
